package com.fancy.action;

import com.fancy.dao.RelationShipFriendsDao;
import com.fancy.response.InvalidRequest;
import com.fancy.response.OKResponse;
import com.fancy.service.PersonChatService;
import com.fancy.service.RelationFriendsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import entity.Friend;
import entity.PersonChatMessage;
import entity.RelationShip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Type;
import java.util.List;

/**
 *
 * @author Jackie
 * @date 2017/10/20
 */

@Controller
@RequestMapping(value = "/person_chat")
public class PersonChat {

    @Autowired
    private PersonChatService personChatService;

    @Autowired
    private Gson gson;

    @Autowired
    RelationFriendsService relationFriendsService;

    @RequestMapping(value = "/sendMessage.do", method = RequestMethod.POST)
    @ResponseBody
    public String sendMessage(@RequestParam(value = "body")String body){
        Type type = new TypeToken<PersonChatMessage>(){}.getType();
        PersonChatMessage personChatMessage = gson.fromJson(body, type);
        if (personChatMessage.person_to_id == null || personChatMessage.person_from_id == null ||
                personChatMessage.content == null) {
            InvalidRequest invalidRequest = new InvalidRequest();
            return gson.toJson(invalidRequest);
        }else {
            boolean result = personChatService.addMessage(personChatMessage);
            if (result) {
                return gson.toJson(new OKResponse());
            } else {
                return gson.toJson(new InvalidRequest());
            }
        }
    }

    @RequestMapping(value = "/getConversation.do")
    @ResponseBody
    public String getConservation(@RequestParam(value = "myID") String myID, @RequestParam(value = "otherID") String otherID){
        List<PersonChatMessage> messages = personChatService.getConversation(myID, otherID);
        String result = gson.toJson(messages);
        return result;
    }

    @RequestMapping(value = "/getMyFriendsCount.do")
    @ResponseBody
    public String getMyFriendsCount(@RequestParam(value = "myID") String myID){
        return gson.toJson(relationFriendsService.getMyFriendsCount(myID));
    }

    @RequestMapping(value = "/getMyFriends.do")
    @ResponseBody
    public String getMyFriends(@RequestParam(value = "myID") String myID){
        List<Friend> friends = relationFriendsService.getMyFriends(myID);
        String result = gson.toJson(friends);
        return result;
    }

    @RequestMapping(value = "/getNotReadMessage.do")
    @ResponseBody
    public String getNotReadMessage(@RequestParam(value = "myID") String myID, @RequestParam(value = "otherID") String otherID){
        return gson.toJson(personChatService.getNotReadMessage(myID, otherID));
    }

    @RequestMapping(value = "/addFriends.do")
    @ResponseBody
    public String addFriends(RelationShip relationShip) {
        relationFriendsService.addFriends(relationShip);
        return null;
    }

    @RequestMapping(value = "/findFriends.do")
    @ResponseBody
    public String findPerson(String friendID) {
        return gson.toJson(relationFriendsService.findPerson(friendID));
    }

}
